﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using NetBay.Core.BusinessEntities.AdvancedSearchCriteria;
using NetBay.Core.BusinessEntities;

namespace Rio.Framework.Pages.AppCode.Models
{
    public class SearchRequest : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _fullTextSearch;
        private bool _useAndOperator = true;
        private bool _saveSearch;
        private bool _enableSaveSearch;

        public Dictionary<long, string> Criterias { get; set; }
        public List<AdvancedSearchCriteriaItem> AdvancedCriterias { get; set; }

        public string FullTextSearch
        {
            get { return _fullTextSearch; }
            set
            {
                _fullTextSearch = value;
                OnPropertyChanged("FullTextSearch");
            }
        }

        public bool UseAndOperator
        {
            get { return _useAndOperator; }
            set
            {
                _useAndOperator = value;
                OnPropertyChanged("UseAndOperator");
            }
        }

        public bool SaveSearch
        {
            get { return _saveSearch; }
            set
            {
                _saveSearch = value;
                OnPropertyChanged("SaveSearch");
            }
        }

        public bool EnableSaveSearch
        {
            get { return _enableSaveSearch; }
            set
            {
                _enableSaveSearch = value;
                OnPropertyChanged("EnableSaveSearch");
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public bool IsSearchRequestValid(bool isAdvancedSearch)
        {
            if (!isAdvancedSearch)
                return (Criterias.Any(c => !String.IsNullOrWhiteSpace(c.Value)) || !String.IsNullOrWhiteSpace(FullTextSearch));
            else
                return (AdvancedCriterias.Count > 0 || !String.IsNullOrWhiteSpace(FullTextSearch));
        }
    }
}
